import moment from 'moment'

const rolesMap = {
  "orgAdmin": '公司管理层',
  "HR": "HR",
  "staff": '成员',
  "superAdmin": '超级管理员',
  "teamAdmin": '团队负责人',
  "projectAdmin":'项目组负责人'
}

const FIELDMAP = {
  fullName: {
    displayName: '姓名',
    type: 'text',
  },
  emailAddress: {
    displayName: '企业邮箱',
    type: 'text',
    isEditable: false,
  },
  mobilePhone: {
    displayName: '手机号',
    type: 'text',
  },
  employeeId: {
    displayName: '工号',
    type: 'text',
  },
  jobTitle: {
    displayName: '职位',
    type: 'text',
    configType: 1,
    isEdit: true,
    updatable: false,
  },
  jobLevel: {
    displayName: '职级',
    type: 'text',
    configType: 2,
    isEdit: true,
    updatable: false,
  },
  nickName: {
    displayName: '昵称',
    type: 'text',
  },
  gender: {
    displayName: '性别',
    type: 'redio',
    typeSpec: {
      pickOptions: [
        {
          pickOptionId: 0,
          optionValue: '男',
        },{
          pickOptionId: 1,
          optionValue: '女',
        },
      ]
    }
  },
  avatarUrl: {
    displayName: '头像',
    type: 'imgFile',
  },
  dateOfBirth: {
    displayName: '生日',
    type: 'text',
  },
  signatureLine: {
    displayName: '个性签名',
    type: 'text',
  },
  selfIntro: {
    displayName: '个人介绍',
    type: 'textArea',
  },
  resume: {
    displayName: '个人简历',
    type: 'editor',
  },
  citizenId: {
    displayName: '身份证号',
    type: 'text',
  },
  degreeLevel: {
    displayName: '学历',
    type: 'text',
    typeSpec: {
      "pickOptions": [
        {
          "optionValue": "小学",
          "pickOptionId": 0
        },
        {
          "optionValue": "初中",
          "pickOptionId": 1          
        },
        {
          "optionValue": "高中/中职",
          "pickOptionId": 2
        },
        {
          "optionValue": "大专",
          "pickOptionId": 3
        },
        {
          "optionValue": "本科",
          "pickOptionId": 4
        },
        {
          "optionValue": "硕士",
          "pickOptionId": 5
        },
        {
          "optionValue": "博士",
          "pickOptionId": 6
        }
      ]
    }
  },
  collegeName: {
    displayName: '毕业院校',
    type: 'text',
  },
  maritalStatus: {
    displayName: '婚姻状况',
    type: 'text',
    typeSpec: {
      pickOptions:[
        {
          "optionValue": "有配偶",
          "pickOptionId": 0
        },
        {
          "optionValue": "无配偶",
          "pickOptionId": 1
        }
      ]
    }
  },
  personalEmail: {
    displayName: '个人邮箱',
    type: 'text',
  },
  livingAddress: {
    displayName: '现住址',
    type: 'address',
  },
  weixinAccount: {
    displayName: '微信号',
    type: 'text',
  },
  weiboAccount: {
    displayName: '微博地址',
    type: 'text',
  },
  qqAccount: {
    displayName: 'QQ号',
    type: 'text',
  },
  linkedinAccount: {
    displayName: 'LinkedIn',
    type: 'text',
  },
  personalWebsite: {
    displayName: '个人网址',
    type: 'text',
  },
  payrollAccount: {
    displayName: '工资卡号',
    type: 'text',
  },
  payrollBank : {
    displayName: '工资卡开户银行',
    type: 'text',
  },
  gongjijinAccount: {
    displayName: '公积金卡号',
    type: 'text',
  },
  shebaoAccount: {
    displayName: '社保账号',
    type: 'text',
  },
  residenceType: {
    displayName: '户籍类型',
    type: 'text',
    typeSpec: {
      pickOptions:[
        {
          "optionValue": "本地城镇",
          "pickOptionId": 0
        },
        {
          "optionValue": "本地农村",
          "pickOptionId": 1
        },
        {
          "optionValue": "外地城镇",
          "pickOptionId": 2
        },
        {
          "optionValue": "外地农村",
          "pickOptionId": 3
        }
      ]
    }
    
  },
  residenceAddress: {
    displayName: '户籍地址',
    type: 'text',
  },
  officialPhoto: {
    displayName: '一寸免冠照片',
    type: 'file',
  },
  citizenIdCopy : {
    displayName: '身份证照片',
    type: 'file',
  },


}

export default FIELDMAP;

export function getFieldGroup(temp, fields = []) {
  const fieldMap = {};
  const otherFieldMap = fields.filter( f => !f.isSystemRequired );
  const groupOther = otherFieldMap
      .filter(field => field.dataType == 1)
      .map(field => ({
        ...field,
        children: otherFieldMap.filter(f => f.containerId == field.profileFieldId)
      }))

  
  for(let i = 0, length = fields.length; i < length; i ++) {
    const fieldItem = fields[i];
    if(!fieldItem.isSystemRequired) continue;
    const fieldMapItem = FIELDMAP[fieldItem.referenceName];
    if(!fieldMapItem) continue;
    fieldMap[fieldItem.referenceName] = {
      ...fieldItem,
      ...fieldMapItem,
    }
  }
  const groupCore = temp.map((item) => {
    const children = [];
    item.children.map((fieldItem) => {
      if(typeof fieldItem === 'object') children.push( fieldItem);
      else if(fieldMap[fieldItem]) children.push(fieldMap[fieldItem]);
    }) 
    return {
      ...item,
      isSystemRequired: 1, 
      isComplete: !!fields.length,
      children: children,
    }
  });
  return groupCore.concat(groupOther);
   
}

const contractTypeMap = {
    1: '实习',
    2: '兼职',
    3: '全职',
  }

export const TemplateMap = [
  {
    displayName: '工作信息',
    isManageOnly: true,
    children: [
      'fullName',
      'emailAddress',
      {
        displayName: '合同状态',
        value: function (v) {
          return v && v.userEmploymentVO? contractTypeMap[v.userEmploymentVO.contractType] : '';
        },
        isEditable: false,
        dataType: -1,
        _dataType: 10,
        referenceName: 'contractType',
        typeSpec: {
          pickOptions: Object.keys(contractTypeMap).map(pickOptionId => ({
            pickOptionId,
            optionValue: contractTypeMap[pickOptionId]
          }))
        }
      },
      {
        displayName: '合同入职时间',
        referenceName: 'enrollDate',
        value: function (v) {
          const {
            userEmploymentVO = {}
          } = v;
          const {
            contractType,
            fulltimeEnrollDate,
            fulltimeResignDate,
            internshipEnrollDate,
            internshipResignDate,
            parttimeEnrollDate,
            parttimeResignDate,
          } = userEmploymentVO;
          if(!v) return '';
          else if(contractType === 1){
            return internshipEnrollDate ? moment(+internshipEnrollDate).format('YYYY-MM-DD') :''
          } else if(contractType === 2){
            return parttimeEnrollDate ? moment(+parttimeEnrollDate).format('YYYY-MM-DD') :''
          } else if(contractType === 3){
            return fulltimeEnrollDate ? moment(+fulltimeEnrollDate).format('YYYY-MM-DD') :''
          }
        },
        isEditable: false,
        dataType: -1,
        _dataType: 4
      },
      {
        displayName: '合同结束时间',
        referenceName: 'resignDate',
        value: function (v) {
          const {
            userEmploymentVO = {}
          } = v;
          const {
            contractType,
            fulltimeEnrollDate,
            fulltimeResignDate,
            internshipEnrollDate,
            internshipResignDate,
            parttimeEnrollDate,
            parttimeResignDate,
          } = userEmploymentVO;
          if(!v) return '';
          else if(contractType === 1){
            return internshipResignDate ? moment(+internshipResignDate).format('YYYY-MM-DD') :''
          } else if(contractType === 2){
            return parttimeResignDate ? moment(+parttimeResignDate).format('YYYY-MM-DD') :''
          } else if(contractType === 3){
            return fulltimeResignDate ? moment(+fulltimeResignDate).format('YYYY-MM-DD') :''
          }
        },
        isEditable: false,
        dataType: -1,
        _dataType: 4
      },
      'employeeId',
      'jobTitle',
      'jobLevel',
      {
        displayName: '汇报对象',
        value: function (v) {
          return v ? v.reporterFullName : '';
        },
        isEditable: false,
        dataType: -1,
        tip: '如需修改汇报对象，请办理调岗',
        updatable: false,
        importable: false
      },
      {
        displayName: '团队',
        value: function (v) {
          return v && v.teamVO ? v.teamVO.teamName : '';
        },
        isEditable: false,
        dataType: -1,
        tip: '如需修改团队，请办理调岗',
        updatable: false,
        importable: false
      },
      {
        displayName: '是否项目组负责人',
        referenceName: 'isTeamAdmin',
        value: function (v) {
          return v && v.teamVO ? v.teamVO.isTeamAdmin : '';
        },
        dataType: -2,
        isEditable: true,
        updatable: false,
        importable: false
      },
      {
        importable: false,
        updatable: false,
        displayName: '系统权限',
        value: function (v) {
          return v.roleNameList && v.roleNameList.length ? v.roleNameList.map(v => rolesMap[v]).join('，') : '';
        },
        isEditable: false,
        dataType: -1,
      },
    ]
  },
  {
    displayName: '个人信息',
    children: [
      'nickName',
      'gender',
      'avatarUrl',
      'dateOfBirth',
      'signatureLine',
      'selfIntro',
      'resume',
      'citizenId',
      'degreeLevel',
      'collegeName',
      'maritalStatus',
    ]
  },
  {
    displayName: '联系方式',
    children: [
      'mobilePhone',
      'personalEmail',
      'livingAddress',
      'weixinAccount',
      'weiboAccount',
      'qqAccount',
      'linkedinAccount',
      'personalWebsite',
    ]
  },
  {
    displayName: '薪酬福利相关',
    children: [
      'payrollAccount',
      'payrollBank',
      'gongjijinAccount',
      'shebaoAccount',
      'residenceType',
      'residenceAddress',
      'officialPhoto',
      'citizenIdCopy',
    ]
  }
]

